import router from 'koa-router';
import BingTile from "../lib/GISServer/WMTSTile/BingTile";
import TianDiTu from "../lib/GISServer/WMTSTile/TianDiTu";

const mapServices = new router({prefix: '/mapServices'});

mapServices
    .get('/BingMaps/WMT/:z/:x/:y.jpeg', async (ctx: any) => {
        ctx.set('Content-Type', 'image/jpeg');
        const z = ctx.params.z;
        const x = ctx.params.x;
        const y = ctx.params.y;

        let [err, buffer] = await BingTile.getTileFileBufferByXYZ(x, y, z);
        ctx.status = 200;
        ctx.body = buffer
    })
    .get('/tianditu-wmts/:mapType/:TileMatrix/:TileCol/:TileRow.png', async (ctx: any) => {
        const { mapType, TileMatrix, TileCol, TileRow } = ctx.params;
        let [err, buffer] = await TianDiTu.getTileFileBuffer(mapType, TileMatrix, TileCol, TileRow);

        if (buffer) {
            ctx.set('Content-Type', 'image/png');
            ctx.body = buffer;
        } else {
            ctx.status = 422;
            ctx.set('Content-Type', 'application/json;charset=utf-8');
            ctx.body = err;
            console.warn('代理 天地图 瓦片失败', mapType, TileMatrix, TileCol, TileRow);
        }
    })


export default mapServices;
